package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.adapter.SearchListViewAdapter;
import com.ledim.adapter.SearchRelatedListViewAdapter;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.bean.SearchResultResponse;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.LedimNetWorkUtils;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.IXListViewListener;
import com.ledim.widget.view.XListView;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class ViewPagerSearchListView extends LinearLayout
        implements IXListViewListener, View.OnClickListener {

    private Context mContext;
    private XListView mListView;
    private View mHeaderView;
    private View mNoSearchLayout;
    private View mNoNetLayout;
    private TextView mReload;
    private View mFooter;
    private View mRelatedLayout;
    private ListView mRelatedListView;
    private String mChannelId;
    private String mKey;
    private SearchListViewAdapter mAdapter;
    private SearchRelatedListViewAdapter mRelatedAdapter;
    private String mRoomId;
    public boolean mIsReload;//控制每个viewpager的页面是否都要加载
    public int mCurrentPage = 1; // 当前第几页

    public ViewPagerSearchListView(Context context) {
        super(context);
        mContext = context;
    }

    public ViewPagerSearchListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ViewPagerSearchListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        if (mListView == null) {
            mListView = (XListView) findViewById(R.id.viewpager_listview);
        }
        if (mFooter == null) {
            mFooter = LayoutInflater.from(mContext)
                                    .inflate(R.layout.footer_search_result_viewpager_listview, null);
            mRelatedLayout = mFooter.findViewById(R.id.related_layout);
            mRelatedListView = (ListView) mFooter.findViewById(R.id.listview_related);
        }

        mHeaderView = View.inflate(mContext, R.layout.no_search_layout, null);
        mNoSearchLayout = mHeaderView.findViewById(R.id.no_search);

        mNoNetLayout = findViewById(R.id.no_network);
        mReload = (TextView) findViewById(R.id.not_network_reload);

        mReload.setOnClickListener(this);
        mNoSearchLayout.setVisibility(View.GONE);

        mListView.setPullRefreshEnable(false);
        mListView.setXListViewListener(this, 0);
        mListView.addHeaderView(mHeaderView);
        mListView.addFooterView(mFooter);
        mListView.setAdapter(null);

    }

    //绑定数据
    public void bindData(ArrayList<LedimChoiceCardBean> data,
                         ArrayList<LedimChoiceCardBean> related, String id, String mKey,
                         String roomId, boolean isReload) {
        this.mKey = mKey;
        this.mChannelId = id;
        this.mIsReload = isReload;
        mRoomId = roomId;
        mCurrentPage = 1;
        if (mChannelId == null) {
            mListView.setVisibility(View.VISIBLE);
            //全部
            if (data.size() != 0) {
                showAllData(data, mListView);
            }
            if (related.size() != 0) {
                showAllRelated(related, mRelatedListView);
            }

            mListView.setPullLoadEnable(true);
            //            mSearchGetModel.getSearchResults(this, mKey, mChannelId,false);

        } else {
            //其他频道
            if (LedimNetWorkUtils.isNetworkAvailable(mContext)) {
                mListView.setVisibility(View.VISIBLE);
                mNoNetLayout.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(mRoomId)) {
                    LedimApiServiceUtils.getSearchResult(mChannelId, mKey, mCurrentPage, 2, observer);
                } else {
                    LedimApiServiceUtils.getSearchResult(mChannelId, mKey, mCurrentPage, 1, observer);
                }
            } else {
                ToastUtil.toast("网络错误");
                mListView.setVisibility(View.GONE);
                mNoNetLayout.setVisibility(View.VISIBLE);
            }
        }
    }

    private LedimHttpObserver<SearchResultResponse> observer =
            new LedimHttpObserver<SearchResultResponse>() {
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }

                @Override
                public void onNext(SearchResultResponse response) {
                    super.onNext(response);
                    mListView.stopRefresh();
                    mListView.stopLoadMore();

                    if (response.success) {
                        if (response.data != null) {
                            //只处理第一页的全部数据
                            if (response.data.data.size() != 0 && mCurrentPage == 1) {
                                showAllData(response.data.data, mListView);
                            }
                            if (response.data.related.size() != 0) {
                                showAllRelated(response.data.related, mRelatedListView);
                            }
                            //加载更多
                            if (response.paged.more) {
                                mListView.setPullLoadEnable(true);
                            } else {
                                mListView.setPullLoadEnable(false);
                            }
                        }
                    }
                }
            };

    /**
     * 显示相关视频
     **/
    private void showAllRelated(ArrayList<LedimChoiceCardBean> related, ListView mRelatedListView) {
        if (related.size() == 0) {
            mRelatedAdapter.setMoreDatas(related);
            mRelatedLayout.setVisibility(View.GONE);
        } else {
            mRelatedLayout.setVisibility(View.VISIBLE);
            if (mRelatedAdapter == null) {
                mRelatedAdapter = new SearchRelatedListViewAdapter(mContext, related, mRoomId);
                mRelatedListView.setAdapter(mRelatedAdapter);
            } else {
                mRelatedAdapter.setMoreDatas(related);
            }
        }
    }

    /**
     * 显示结果视频
     **/
    private void showAllData(ArrayList<LedimChoiceCardBean> data, XListView mListView) {
        if (data.size() == 0) {
            mAdapter.setMoreDatas(data);
        } else {
            if (mAdapter == null) {
                mAdapter = new SearchListViewAdapter(mContext, data, mRoomId);
                mListView.setAdapter(mAdapter);
            } else {
                mAdapter.setMoreDatas(data);
            }
        }
    }

    @Override
    public void onRefresh(int id) {
    }

    @Override
    public void onLoadMore(int id) {
        if (!TextUtils.isEmpty(mRoomId)) {
            LedimApiServiceUtils.getSearchResult(mChannelId, mKey, ++mCurrentPage, 2, observer);
        } else {
            LedimApiServiceUtils.getSearchResult(mChannelId, mKey, ++mCurrentPage, 1, observer);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.not_network_reload:
                if (LedimNetWorkUtils.isNetworkAvailable(mContext)) {
                    mListView.setVisibility(View.VISIBLE);
                    mNoNetLayout.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(mRoomId)) {
                        LedimApiServiceUtils.getSearchResult(mChannelId, mKey, 1, 2, observer);

                    } else {
                        LedimApiServiceUtils.getSearchResult(mChannelId, mKey, 1, 1, observer);
                    }
                } else {
                    ToastUtil.toast("网络错误");
                    mListView.setVisibility(View.GONE);
                    mNoNetLayout.setVisibility(View.VISIBLE);
                }
                break;
        }
    }
}
